﻿@model OT.Areas.Admin.Models.Tests
<div class="modal-content">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="fa fa-times"></i></button>
        <h3 id="myModalLabel">Create Test</h3>
    </div>
    <div class="modal-body">
        <div id="alert_modal">
            @Html.ValidationSummary()
            @Html.Hidden("QuetionIDs", null, new { id = "QuetionIDs" })
        </div>
          <div class="form-vertical" role="form">
                <div class="form-group">
                    <div class="col-lg-13">
                        <div id="divMsgCre"></div>
                    </div>
                </div>
            </div>
            <div class="form-vertical" role="form">

                <div class="form-group" id="divCategoryName">
                    <label class="col-lg-2 control-label" style="text-align:right">Category Name</label>
                    <div class="col-lg-4">
                        @Html.DropDownListFor(model => model.CategoryID, new SelectList(Model.cat, "CategoryID", "CategoryName"), "--- Select ---", new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => model.CategoryID)
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-lg-2 control-label" style="text-align:right">Available Question</label>
                    <div class="col-lg-4">
                        <div id="TotAvail" style="color: green; font-size: 16px;"></div>
                    </div>
                </div>
            </div>
            <div class="clearfix"></div>
            <div class="form-vertical" role="form">
                <div class="form-group" id="divSubject">
                    <label class="col-lg-2 control-label" style="text-align:right">Subject</label>
                    <div class="col-lg-4">
                        <select id="SubjectID" class="form-control">
                            <option value="0">--Select--</option>
                        </select>
                    </div>
                </div>
                <div class="form-group" id="divTotalQuestion">
                    <label class="col-lg-2 control-label" style="text-align:right">Total Question</label>
                    <div class="col-lg-2">
                        <input class="form-control" type="text" placeholder="No of Question" id="TotalQuestion" maxlength="2">
                    </div>
                    <div class="col-lg-2">
                        <a href="/Student/MyTest/ShowAllQuestionCreate?subjectId=" id="Custom">Custom Question</a>
                    </div>
                </div>
            </div>
            <div class="clearfix"></div>
            <div class="form-vertical" role="form">
                <div class="form-group" id="divTopic">
                    <label class="col-lg-2 control-label" style="text-align:right">Topic</label>
                    <div class="col-lg-4">
                        <select id="TopicID" class="form-control">
                            <option value="0">--Select--</option>
                        </select>
                    </div>
                </div>
                <div class="form-group" id="divPassingScore">
                    <label class="col-lg-2 control-label" style="text-align:right">Passing Score</label>
                    <div class="col-lg-4">
                        <input class="form-control" type="text" placeholder="Passing Score for Test" id="PassingScore" maxlength="3">
                    </div>
                </div>
            </div>
            <div class="clearfix"></div>
            <div class="form-vertical" role="form">
                <div class="form-group" id="divTestType">
                    <label class="col-lg-2 control-label" style="text-align:right">Test Type</label>
                    <div class="col-lg-4">
                        @Html.DropDownListFor(model => model.TestTypeID, new SelectList(Model.TestTypes, "TestTypeID", "TestType1"), "-- Select --", new { @class = "form-control" })
                        @Html.ValidationMessageFor(model => model.TestTypeID)
                    </div>
                </div>
                <div class="form-group" id="divDuration" style="text-align:right">
                    <label class="col-lg-2 control-label">Duration Of Test</label>
                    <div class="col-lg-4">
                        <input class="form-control" type="text" placeholder="Enter Duration of Exam [in Minute]" id="Duration" maxlength="3">
                    </div>
                </div>
            </div>
            <div class="clearfix"></div>
            <div class="form-vertical" role="form">
                <div class="form-group" id="divTestName">
                    <label class="col-lg-2 control-label" style="text-align:right">Test Name</label>
                    <div class="col-lg-4">
                        <input class="form-control" type="text" placeholder="Enter Test Name" id="TestName" maxlength="30">
                    </div>
                </div>
                <div class="form-group" id="divPositiveMark">
                    <label class="col-lg-2 control-label" style="text-align:right">Positive Mark</label>
                    <div class="col-lg-4">
                        <input class="form-control" type="text" placeholder="Positive Mark ex.1,2,3..." id="PositiveMark" maxlength="3">
                    </div>
                </div>
            </div>
            <div class="clearfix"></div>
            <div class="form-vertical" role="form">
                <div class="form-group" id="divDescription">
                    <label class="col-lg-2 control-label" style="text-align:right">Description</label>
                    <div class="col-lg-4">
                        <textarea class="form-control" placeholder="Enter Description" id="Description" rows="1" maxlength="50"></textarea>
                    </div>
                </div>
                <div class="form-group" id="divNegativeMark">
                    <label class="col-lg-2 control-label" style="text-align:right">Negative Mark</label>
                    <div class="col-lg-4">
                        <input class="form-control" type="text" placeholder="Negative Mark ex.1,2,3,..." id="NegativeMark" maxlength="3">
                    </div>
                </div>
            </div>
            <hr />
        </div>

        <div class="clearfix"></div>
   
        <div class="modal-footer">
            <button class="btn btn-default" type="submit" id="btnCreate"><i class="icon-cog"></i>Save</button>
            <button class="btn btn-default" data-dismiss="modal" aria-hidden="true"><i class="icon-off"></i>Cancel</button>
        </div>
    </div>
    <script type="text/javascript">
        $("#btnCreate").click(function (e) {
            var TestTypeID = $("#TestTypeID option:selected").val();
            var CategoryID = $("#CategoryID option:selected").val();
            var SubjectID = $("#SubjectID option:selected").val();
            var TopicID = $("#TopicID option:selected").val();
            var TestName = $("#TestName").val();
            var Description = $("#Description").val();
            var TotalQuestion = $("#TotalQuestion").val();
            var Duration = $("#Duration").val();
            var PassingScore = $("#PassingScore").val();
            var PositiveMark = $("#PositiveMark").val();
            var NegativeMark = $("#NegativeMark").val();
            var Status = true;
            var QuestionIDs = $("#QuetionIDs").val();
            e.preventDefault();
            if (TestTypeID == 0 || TestName == '' || CategoryID == 0 || SubjectID == 0 || TopicID == 0 || TotalQuestion == '' || Duration == '' || PassingScore == '' || PositiveMark == '' || NegativeMark == '') {
                showMsg('divMsgCre', 'Please Enter Value in Required Fields', 'danger');
                (TestTypeID == 0) ? $('#divTestType').addClass('has-error') : $('#divTestType').removeClass('has-error');
                (CategoryID == 0) ? $('#divCategoryName').addClass('has-error') : $('#divCategoryName').removeClass('has-error');
                (SubjectID == 0) ? $('#divSubject').addClass('has-error') : $('#divSubject').removeClass('has-error');
                (TopicID == 0) ? $('#divTopic').addClass('has-error') : $('#divTopic').removeClass('has-error');
                (TestName == '') ? $('#divTestName').addClass('has-error') : $('#divTestName').removeClass('has-error');
                (TotalQuestion == '') ? $('#divTotalQuestion').addClass('has-error') : $('#divTotalQuestion').removeClass('has-error');
                (Duration == '') ? $('#divDuration').addClass('has-error') : $('#divDuration').removeClass('has-error');
                (PassingScore == '') ? $('#divPassingScore').addClass('has-error') : $('#divPassingScore').removeClass('has-error');
                (PositiveMark == '') ? $('#divPositiveMark').addClass('has-error') : $('#divPositiveMark').removeClass('has-error');
                (NegativeMark == '') ? $('#divNegativeMark').addClass('has-error') : $('#divNegativeMark').removeClass('has-error');
            }
            else {
                var totavail = Number($("#TotAvail").text());
                var selectque = Number($('#TotalQuestion').val());
                if (selectque > totavail) {
                    showMsg('divMsgCre', 'Total Question is Greater than Available Question', 'danger');
                }
                else {
                    $.ajax({
                        url: "/Student/MyTest/Create?TestName=" + TestName + "&TestTypeID=" + TestTypeID + "&CategoryID=" + CategoryID + "&SubjectID=" + SubjectID + "&TopicID=" + TopicID + "&Description=" + Description + "&TotalQuestion=" + TotalQuestion + "&Duration=" + Duration + "&PassingScore=" + PassingScore + "&PositiveMark=" + PositiveMark + "&NegativeMark=" + NegativeMark + "&Status=" + Status + "&QuestionIDs=" + QuestionIDs,

                        type: "POST",
                        success: function (result) {
                            if (result != "NotValid") {
                                $('#modalDiv').modal('hide');
                                showMsg('divMsgIndex', 'Record Saved Successfuly', 'success');
                                loadPage(1, 10, '');
                            }
                            else {
                                showMsg('divMsgCre', 'Record Not Saved', 'danger');
                            }
                        },
                        error: function (data) {
                        }
                    });
                }
            }
            return false;
        });
        $("#TotalQuestion").keyup(function (e) {
            var actual = Number($(this).val());
            var tot = Number($("#TotAvail").html());
            if (tot < actual) {
                $("#ErrorMsg").css({ "display": "initial" });
                $("#ErrorMsg").html("Total Question should not be more than Available Question");
              //  showMsg('divMsgCre', 'Total Question should not be more than Available Question', 'danger');
            }
            else {
                $("#ErrorMsg").css({ "display": "none" });
            }
        });

        $("#TotalQuestion").keypress(function (e) {
            if ((e.which < 48 || e.which > 57)) {
                if (e.which == 8 || e.which == 46 || e.which == 0) {
                    $("#ErrorMsg").css({ "display": "none" });
                    return true;
                }
                else {
                    $("#ErrorMsg").css({ "display": "initial" });
                    showMsg('divMsgCre', 'Enter only numbers 0-9', 'info');
                    $("#ErrorMsg").html("Enter only numbers 0-9");
                    return false;
                }
            }
        });

        $("#Duration").keypress(function (e) {
            if ((e.which < 48 || e.which > 57)) {
                if (e.which == 8 || e.which == 46 || e.which == 0) {
                    $("#ErrorMsg1").css({ "display": "none" });
                    return true;
                }
                else {
                    $("#ErrorMsg1").css({ "display": "initial" });
                    $("#ErrorMsg1").html("Enter only numbers 0-9");
                    showMsg('divMsgCre', 'Enter only numbers 0-9', 'info');
                    return false;
                }
            }
        });

        $("#PassingScore").keypress(function (e) {
            if ((e.which < 48 || e.which > 57)) {
                if (e.which == 8 || e.which == 46 || e.which == 0) {
                    $("#ErrorMsg2").css({ "display": "none" });
                    return true;
                }
                else {
                    $("#ErrorMsg2").css({ "display": "initial" });
                    //showMsg('divMsgCre', 'Enter only numbers 0-9', 'info');
                    return false;
                }
            }
        });

        $("#PositiveMark").keypress(function (e) {
            if ((e.which < 48 || e.which > 57)) {
                if (e.which == 8 || e.which == 46 || e.which == 0) {
                    $("#ErrorMsg3").css({ "display": "none" });
                    return true;
                }
                else {
                    $("#ErrorMsg3").css({ "display": "initial" });
                    showMsg('divMsgCre', 'Enter only numbers 0-9', 'info');
                    return false;
                }
            }
        });

        $("#NegativeMark").keypress(function (e) {
            if ((e.which < 48 || e.which > 57)) {
                if (e.which == 8 || e.which == 46 || e.which == 0) {
                    $("#ErrorMsg4").css({ "display": "none" });
                    return true;
                }
                else {
                    $("#ErrorMsg4").css({ "display": "initial" });
                    showMsg('divMsgCre', 'Enter only numbers 0-9', 'info');
                    return false;
                }
            }
        });

        $("#Custom").click(function (e) {
            var id = $("#SubjectID option:selected").val();
            var tid = $("#TopicID option:selected").val();
            e.preventDefault();
            if (id == 0 || tid == 0) {
                showMsg('divMsgCre', 'Must Be Select Subject And Topic First', 'danger');
            }
            else {
               
                $("#CustomQuestion").load(this.href + id + "&topicid=" + tid, function () {
                    $('#modalQsDiv').modal({
                        backdrop: 'static'
                    }, 'show');
                });
            }
        });

        $("#CategoryID").change(function (e) {
            var CategoryID = $(this).val();
            if (CategoryID != 0) {
                $.getJSON("/Student/MyTest/Subjects", { CategoryID: CategoryID },
                       function (classesData) {
                           var select = $("#SubjectID");
                           select.empty();
                           select.append($('<option/>', {
                               value: 0,
                               text: "----Select Subject----"
                           }));
                           $.each(classesData, function (index, itemData) {
                               select.append($('<option/>', {
                                   value: itemData.Value,
                                   text: itemData.Text
                               }));
                           });
                       });
            }
            else {
                var select = $("#SubjectID");
                select.empty();
                select.append($('<option/>', {
                    value: 0,
                    text: "----Select Subject----"
                }));
                var select1 = $("#TopicID");
                select1.empty();
                select1.append($('<option/>', {
                    value: 0,
                    text: "----Select Topic----"
                }));
            }
            return false;
        });

        $("#SubjectID").change(function (e) {
            var SubjectID = $(this).val();
            if (SubjectID != 0) {

                $.getJSON("/Student/MyTest/Topics", { SubjectID: SubjectID },
                       function (classesData) {
                           var select = $("#TopicID");
                           select.empty();
                           select.append($('<option/>', {
                               value: 0,
                               text: "----Select Topic----"
                           }));
                           $.each(classesData, function (index, itemData) {

                               select.append($('<option/>', {
                                   value: itemData.Value,
                                   text: itemData.Text
                               }));
                           });
                       });
            }
            else {
                var select = $("#TopicID");
                select.empty();
                select.append($('<option/>', {
                    value: 0,
                    text: "----Select Topic----"
                }));
            }
            return false;
        });

        $("#TopicID").change(function (e) {
            var TopicID = $(this).val();
            var SubjectID = $("#SubjectID option:selected").val();

            if (TopicID == '')
                TopicID = '0';
            $("#totalAvailQue").css({ "display": "none" });

            $.get("/Instructor/Test/GetAvailableQuestion", { SubjectID: SubjectID, TopicID: TopicID }, function (data) {
                $("#TotAvail").html(data);
            });
            return false;
        });
    </script>
